/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.web;

import java.util.List;
import java.util.Set;

import com.javaeye.delivery.dto.DeliveryOrder;
import com.javaeye.delivery.dto.DeliveryOrderDetail;
import com.javaeye.delivery.service.IDeliveryOrderService;
import com.javaeye.common.web.PageInfo;
import com.javaeye.common.util.ListUtil;
import com.javaeye.common.util.ListItem;

import com.opensymphony.xwork2.ActionSupport;

public class DeliveryOrderAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7408989332825417843L;
	
	private OrderCondition condition;
	
	private PageInfo pageInfo;
	
	private List<DeliveryOrder> orderList;
	
	private String orderId;
	
	private List<ListItem> logisticsTypeList;
	
	/**
	 * 成品交运单信息
	 */
	private DeliveryOrder order;
	
	/**
	 * 成品交运单明细ID
	 */
	private List<Integer> detailIds;
	
	/**
	 * 成品交运单明细产品数量
	 */
	private List<Integer> productNums;
	
	/**
	 * 成品交运单明细产品件数
	 */
	private List<Integer> packageNumbers;
	
	/**
	 * 成品交运单明细产品金额
	 */
	private List<Double> productAmounts;
	
	/**
	 * 成品交运单明细产品备注
	 */	
	private List<String> deliveryInfos;
	
	private IDeliveryOrderService service;
	
	public DeliveryOrderAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new OrderCondition();
		condition.setCustomName("");
		condition.setProductName("");
	}
	
	/**
	 * 查询成品交运单列表
	 * @return
	 * @throws Exception
	 */
	public String queryOrderList() throws Exception {
		orderList = service.getOrderList(condition, pageInfo);
		return SUCCESS;
	}
	
	/**
	 * 查询成品交运单信息
	 * @return
	 * @throws Exception
	 */
	public String queryDeliveryOrderInfo() throws Exception {
		order = service.getDeliveryOrder(orderId);
		return SUCCESS;
	}
	
	public String queryOrderForEdit() throws Exception {
		logisticsTypeList = ListUtil.logisticsTypeList();
		return queryDeliveryOrderInfo();
	}
	
	/**
	 * 保存成品交运单
	 * @return
	 * @throws Exception
	 */
	public String saveOrder() throws Exception {
		if (1 == Integer.parseInt(order.getLogisticsType())) {
			if (order.getLogisticsId() == 0) {
				this.addActionError("无法保存，公司合约货运，就必须选择物流公司！");
				queryOrderForEdit();
				return INPUT;
			}
		}
		DeliveryOrder dbOrder = service.getDeliveryOrder(orderId);
		dbOrder.setOrderNo(order.getOrderNo());
		dbOrder.setDeliveryDate(order.getDeliveryDate());
		dbOrder.setReceivers(order.getReceivers());
		dbOrder.setTelephone(order.getTelephone());
		dbOrder.setAddress(order.getAddress());
		dbOrder.setTotleAmount(order.getTotleAmount());
		dbOrder.setLogisticsType(order.getLogisticsType());
		dbOrder.setLogisticsId(order.getLogisticsId());
		dbOrder.setLogisticsName(order.getLogisticsName());
		dbOrder.setTotleWeight(order.getTotleWeight());
		dbOrder.setTotleVolume(order.getTotleVolume());
		
		Set<DeliveryOrderDetail> details = dbOrder.getOrderDetails();
		int i = 0;
		for (DeliveryOrderDetail detail : details) {
			detail.setNumber(productNums.get(i));
			detail.setPackageNumber(packageNumbers.get(i));
			detail.setAmount(productAmounts.get(i));
			detail.setDeliveryInfo(deliveryInfos.get(i));
			dbOrder.addDetail(detail);
			i++;
		}
		service.save(dbOrder);
		return SUCCESS;
	}

	public OrderCondition getCondition() {
		return condition;
	}

	public void setCondition(OrderCondition condition) {
		this.condition = condition;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public List<DeliveryOrder> getOrderList() {
		return orderList;
	}

	public void setOrderList(List<DeliveryOrder> orderList) {
		this.orderList = orderList;
	}

	public String getOrderId() {
		return orderId;
	}

	public void setOrderId(String orderId) {
		this.orderId = orderId;
	}

	public DeliveryOrder getOrder() {
		return order;
	}

	public void setOrder(DeliveryOrder order) {
		this.order = order;
	}

	public List<Integer> getDetailIds() {
		return detailIds;
	}

	public void setDetailIds(List<Integer> detailIds) {
		this.detailIds = detailIds;
	}

	public List<Integer> getProductNums() {
		return productNums;
	}

	public void setProductNums(List<Integer> productNums) {
		this.productNums = productNums;
	}

	public List<Integer> getPackageNumbers() {
		return packageNumbers;
	}

	public void setPackageNumbers(List<Integer> packageNumbers) {
		this.packageNumbers = packageNumbers;
	}

	public List<Double> getProductAmounts() {
		return productAmounts;
	}

	public void setProductAmounts(List<Double> productAmounts) {
		this.productAmounts = productAmounts;
	}

	public List<String> getDeliveryInfos() {
		return deliveryInfos;
	}

	public void setDeliveryInfos(List<String> deliveryInfos) {
		this.deliveryInfos = deliveryInfos;
	}

	public void setService(IDeliveryOrderService service) {
		this.service = service;
	}
	
	public List<ListItem> getLogisticsTypeList() {
		return logisticsTypeList;
	}
	
	public void setLogisticsTypeList( List<ListItem> logisticsTypeList) {
		this.logisticsTypeList = logisticsTypeList;
	}
	
}
